package com.zhuhjay.common.web.config;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static com.zhuhjay.common.base.constant.ApplicationConstant.FILE_RESOURCE_PREFIX;
import static com.zhuhjay.common.base.constant.ApplicationConstant.PATH_SEPARATOR;

/**
 * @author ZhuHJay
 * @date 2023/2/25 12:19
 */
@Configuration
@EnableAsync
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${generic-boot.system.profile-location}")
    private String profileLocation;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 创建资源目录
        FileUtil.mkdir(profileLocation);
        /* 添加自定义静态路径 */
        registry.addResourceHandler(
                // 配置静态资源路径 /profile/**
                "/profile/**"
        ).addResourceLocations(
                // 配置静态资源映射路径 file:profile/
                FILE_RESOURCE_PREFIX + profileLocation + PATH_SEPARATOR
        );
        /* 配置knife4j 显示文档 */
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        /* 公共部分内容 */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
